package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC928 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int minMalwareSpread(int[][] graph, int[] initial) {
            int N = graph.length;
            UF dsu = new UF(N);

            // clean[u] == 1 if its a node in the graph not in initial.
            int[] clean = new int[N];
            Arrays.fill(clean, 1);
            for (int x : initial) clean[x] = 0;

            for (int u = 0; u < N; ++u)
                if (clean[u] == 1)
                    for (int v = 0; v < N; ++v)
                        if (clean[v] == 1)
                            if (graph[u][v] == 1)
                                dsu.union(u, v);

            // dsu now represents the components of the graph without
            // any nodes from initial.  Let's call this graph G.
            int[] count = new int[N];
            Map<Integer, Set<Integer>> nodeToCompo = new HashMap();
            for (int u : initial) {
                Set<Integer> components = new HashSet();
                for (int v = 0; v < N; ++v)
                    if (clean[v] == 1) {
                        if (graph[u][v] == 1)
                            components.add(dsu.find(v));
                    }

                nodeToCompo.put(u, components);
                for (int c : components)
                    count[c]++;
            }

            // For each node u in initial, nodeToCompo.get(u)
            // now has every component from G that u neighbors.

            int ans = -1, ansSize = -1;
            for (int u : nodeToCompo.keySet()) {
                Set<Integer> components = nodeToCompo.get(u);
                int score = 0;
                for (int c : components)
                    if (count[c] == 1) // uniquely infected
                        score += dsu.size[c];   //此处c可以认为是core的概念，集团核心，代表一个集团

                if (score > ansSize || score == ansSize && u < ans) {
                    ansSize = score;
                    ans = u;
                }
            }

            return ans;
        }
    }

    public class UF {
        // 连通分量个数
        private int count;
        // 存储一棵树
        private int[] parent;
        // 记录树的“重量”
        private int[] size;

        public UF(int n) {
            this.count = n;
            parent = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }

        /* 将 p 和 q 连接 */
        public void union(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            if (rootP == rootQ)
                return;

            // 小树接到大树下面，较平衡
            if (size[rootP] > size[rootQ]) {
                parent[rootQ] = rootP;
                size[rootP] += size[rootQ];
            } else {
                parent[rootP] = rootQ;
                size[rootQ] += size[rootP];
            }
            count--;
        }

        /* 判断 p 和 q 是否连通 */
        public boolean connected(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            return rootP == rootQ;
        }

        private int find(int x) {
            while (parent[x] != x) {
                // 进行路径压缩
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }

        /* 返回图中有多少个连通分量 */
        public int count() {
            return count;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC928 an = new LC928();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
